package org.robot.scm.pojos.platform;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.LocalTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class VideoBaseNotify extends BaseNotify {

    private String url;

    /**
     * 创建小时 2024-03-15 08:00:00
     */
    private String createHour;

    @SneakyThrows
    @Override
    public TargetNotify robotToNotifyBean(String robotId, BaseConfig config) {
        String content = config.robotFile(robotId, URLEncoder.encode(url, StandardCharsets.UTF_8.name()));
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.POST)
                .channelEnum(ChannelEnum.ROBOT)
                .notifyUrl(config.robotUrl())
                .topic(config.get().getNotifyChannel().getRobot().getBodyFile())
                .content(content)
                .build();
    }

    @Override
    public boolean equal(BaseNotify another) {
        // 当前时间小于9点不提醒，每小时执行一次的话，应该是8:00~9:00之间才会提醒
        LocalDateTime currentTime = LocalDateTime.now();
        LocalTime eightOclock = LocalTime.of(8, 0, 0);
        if (currentTime.toLocalTime().isBefore(eightOclock)) {
            return true;
        }
        // 按小时生成只会每小时提醒一次
        return StringUtils.equals(((VideoBaseNotify) another).getCreateHour(), createHour);
    }

}
